#!/bin/sh
#desc:Configure control ports
#package:odcontrol
#type:local

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later

CTRLPATH="/var/opendomo/control"
CFGPATH="/etc/opendomo/control"
ZONEDIR="/etc/opendomo/zones"


# Loading zones list
mkdir -p $ZONEDIR
cd $ZONEDIR
for z in *; do
	if test "$z" != "*"
	then
		# Necesitamos la primera zona ""
		ZONES="$ZONES,$z:$z"
	else
		ZONES="main"
	fi
done

POSSV="on-off,ON-OFF,00-10-20-30-40-50-60-70-80-90-100"

TAGDIR=/etc/opendomo/tags
if ! test -d $TAGFILE
then
	/usr/local/opendomo/manageTags.sh
fi

cd $TAGDIR
for t in *; do
	if test -z $TAGS
	then
		TAGS="$t:$t"
	else
		TAGS="$TAGS,$t:$t"
	fi
done

# Tipos de puerto de entrada
TYPES="switch:switch,pulser:pulser,analog:analog"

cd $CTRLPATH
#PORTS=`find -type p | sed 's/\.\///g' | sort`

PORT=`echo $1 | sed 's/\./\//'`

# -----------------------------------------------------------------------------
# Sin parámetros, primera ejecución -> mostrar lista de puertos
# -----------------------------------------------------------------------------
found=0
if test -z "$PORT" || ! test -e "/var/opendomo/control/$PORT"; then
	echo "#> Select port"
	echo "list:`basename $0`	listbox selectable"
	cd $CTRLPATH
	for device in *; do
		if test "$device" != "*"; then 
			cd $device
			mkdir -p $CFGPATH/$device
			echo "	$device	$device	separator"
			for port in *; do
				if test -f $CFGPATH/$device/$port.info; then
					name="$port"
					if test -e $CFGPATH/$device/$port.desc
					then
						name="`cat $CFGPATH/$device/$port.desc`"
					fi
					echo "	-$device/$port	$name	port sub"
					found=1
				fi
			done
			cd ..
		fi
	done

	if test "$found" = 0
	then
		echo "#WARN: No ports found"
		echo "actions:"
		if test -x /usr/local/opendomo/manageControlDevices.sh; then
			echo "	manageControlDevices.sh	Add control device"
		fi
		echo
		exit 0
	fi
	echo "action:"
	echo
else
# -----------------------------------------------------------------------------
# Con un parámetro, segunda ejecución -> mostrar formulario
# -----------------------------------------------------------------------------
	if test -z "$2";
	then
		mkdir -p $CFGPATH/`echo $PORT | cut -f1 -d/` 2>/dev/null
	
		# Obtenemos el nombre del dispositivo
		if test -f $CFGPATH/$PORT.desc; then
			name=`cat $CFGPATH/$PORT.desc`
		else
			name=$PORT
			echo $name > $CFGPATH/$PORT.desc
		fi
	

		# Si existe configuración, la usamos. Si no recurrimos a la
      # configuración por defecto
		if test -f $CFGPATH/$PORT.info
		then
			. $CFGPATH/$PORT.info
		else
			chmod u+w $CTRLPATH/$PORT.info 2>/dev/null
			. $CTRLPATH/$PORT.info
		fi
		echo "#> Configure ${way}put port"
		echo "form:`basename $0`"
		echo "	port	Port	readonly	$1"
		echo "	name	Name	text	$name"
		echo "	status	Status	list[enabled:enabled,disabled:disabled]	enabled"
		echo "	way	way	hidden	$way"
		
		# Si es de salida:
		if test "$way" = "out"
		then
			pv=`echo $values| sed 's/,/-/g'`
			echo "	values	Possible values	list[$POSSV]	$pv"
			echo "	zone	Zone	list[$ZONES]	$zone"
			echo "	tags	Tags	list[$TAGS]	$tags"
	
		# Si es de entrada:
		elif test "$way" = "in"
		then
			echo "	units	Units	text	$units"
			echo "	zone	Zone	list[$ZONES]	$zone"
			echo "	type	Type	list[$TYPES]	$type"
			echo "	tags	Tags	list[$TAGS]	$tags"
		else
			echo "#ERR: No way specified"
		fi


# -----------------------------------------------------------------------------
# Con varios parámetros, tercera ejecución -> guardar
# -----------------------------------------------------------------------------
	else

		# Create new configuration
		rm -f $CFGPATH/$PORT.info
		touch $CFGPATH/$PORT.info
		chmod 700 $CFGPATH/$PORT.info

		pv=`echo $values | sed 's/-/,/g'`
		name=`echo $2 | sed 's/+/ /g'`
		echo $name > $CFGPATH/$PORT.desc

		echo "status='$3'"  	>> $CFGPATH/$PORT.info
		echo "way='$way'"    	>> $CFGPATH/$PORT.info
		echo "units='$units'" 	>> $CFGPATH/$PORT.info
		echo "values='$pv'" 	>> $CFGPATH/$PORT.info
		echo "zone='$zone'" 	>> $CFGPATH/$PORT.info
		echo "tags='$tags'" 	>> $CFGPATH/$PORT.info
		echo "type='$type'" 	>> $CFGPATH/$PORT.info

		echo "#INF: Configuration saved"
		echo
		chmod o+rx $CFGPATH/$PORT.info
		/usr/local/opendomo/configureControlPorts.sh
	fi
fi
echo
